//
//  LMCustomAttachment.swift
//  Pods
//
//  Created by yunxiwangluo on 2020/7/9.
//

import UIKit
import NIMAVChat

class LMCustomAttachment: NSObject,NIMCustomAttachment {
    
    var jsonString: String?
    var type: Int?
    
    init(type: Int,jsonString: String) {
        super.init();
        self.jsonString = jsonString;
        self.type = type;
    }
    
    
    func encode() -> String {
        
        let dict: Dictionary<String,Any> = [
            "key":self.type,
            "data":["data":self.jsonString]
        ];
        
       return LMTools.dicValueString(dict) ?? "";


        //  let jsonData = try? JSONSerialization.data(withJSONObject: jsonStr, options: .fragmentsAllowed)
        //return String(data: jsonData!, encoding: String.Encoding.utf8) ?? ""

        
    }
    
}


class LMCustomAttachmentCoding: NSObject,NIMCustomAttachmentCoding{
    
    func decodeAttachment(_ content: String?) -> NIMCustomAttachment? {
         
        let dict:Dictionary<String,Any> = LMTools.stringValueDic(content ?? "") ?? Dictionary<String,Any>();
        
        let data = dict["data"] as! Dictionary<String,Any>;
        
        return LMCustomAttachment(type: dict["key"] as! Int, jsonString: LMTools.dicValueString(data) ?? "");
        
    }
}
